const gulp = require('gulp');
const clean = require('gulp-clean');
const concat = require('gulp-concat');
const autoprefixer = require('gulp-autoprefixer');
const replace = require('gulp-replace');
const uglify = require('gulp-uglify');
const filter = require('gulp-filter');
const minifyCss = require('gulp-minify-css');
const htmlreplace = require('gulp-html-replace');
const rev = require('gulp-rev');
const revCollector = require('gulp-rev-collector');
const runSequence = require('run-sequence');

gulp.task('clean', function () {
    return gulp.src(['src/main/webapp/dist','src/main/webapp/prodpage'], {read: false})
        .pipe(clean());
});

gulp.task('build-concat', function () {
    gulp.src(['src/main/webapp/script/common/common.js','src/main/webapp/script/common/util.js'])
        .pipe(concat('tx_comm.min.js'))
        //压缩js
        .pipe(uglify())
        .pipe(rev())
        .pipe(gulp.dest('src/main/webapp/dist/script'))
        .pipe(rev.manifest())
        .pipe(gulp.dest('src/main/webapp/dist/rev/concat'));
});

gulp.task('build-js', function () {

    return gulp.src(['src/main/webapp/script/**/*.js'])

    //压缩js
        .pipe(uglify())
        .pipe(rev())
        .pipe(gulp.dest('src/main/webapp/dist/script'))
        .pipe(rev.manifest())
        .pipe(gulp.dest('src/main/webapp/dist/rev/js'));

});

gulp.task('build-static', function () {

    return gulp.src(['src/main/webapp/static/**/*.*'])
        .pipe(gulp.dest('src/main/webapp/dist/static'))

});

gulp.task('build-css', function () {

    return gulp.src(['src/main/webapp/css/**/*.css'])

    //压缩css
        .pipe(autoprefixer({
            browsers: ['last 2 versions'],
            cascade: true,
            add: true,
            remove: true,
            flexbox: true
        }))
        .pipe(minifyCss())
        .pipe(rev())
        .pipe(gulp.dest('src/main/webapp/dist/css'))
        .pipe(rev.manifest())
        .pipe(gulp.dest('src/main/webapp/dist/rev/css'));

});
gulp.task('build-font', function () {

    return gulp.src(['src/main/webapp/css/**/*.{eot,svg,ttf,woff}'])
        .pipe(gulp.dest('src/main/webapp/dist/css'))

});
gulp.task('build-images', function () {

    return gulp.src(['src/main/webapp/image/**/*.{png,jpg,gif,ico}'])

        // .pipe(rev())
        .pipe(gulp.dest('src/main/webapp/dist/image'))
        // .pipe(rev.manifest())
        // .pipe(gulp.dest('src/main/webapp/dist/rev/images'));
});

gulp.task('build-jsp', function () {
    return gulp.src(['src/main/webapp/dist/rev/**/rev-manifest.json', 'src/main/webapp/page/**/*.jsp'])
        .pipe(htmlreplace({
            'js': '${ctx}/script/tx_comm.min.js'
        }))
        .pipe(revCollector())
        .pipe(replace('${ctx}/css', '${ctx}/dist/css'))
        .pipe(replace('${ctx}/script', '${ctx}/dist/script'))
        .pipe(replace('${ctxStatic}', '${ctx}/dist/static'))
        .pipe(replace('${ctx}/image', '${ctx}/dist/image'))
        .pipe(gulp.dest('src/main/webapp/prodpage'));
});


gulp.task('default', function (callback) {
    runSequence(
        'clean',
        'build-concat',
        'build-js',
        'build-static',
        'build-css',
        'build-font',
        'build-images',
        'build-jsp',
        callback);
});
//gulp.task('watch', function () {  
//    gulp.watch('**', ['default']);  
//});  